<template>
  <div>
    <div>
      <el-card>
        <div class="body">
          <elform
            @search="search"
            @add="add"
            :btnName="'新增学科'"
            :placeholder="''"
            :label="'学科名称'"
          ></elform>

          <elalert :counts="counts"></elalert>

          <el-table :data="list" style="width: 100%">
            <el-table-column prop="id" label="序号" width="52px" />
            <el-table-column prop="subjectName" label="学科名称" />
            <el-table-column prop="username" label="创建者" />
            <el-table-column prop="addDate" label="创建日期" width="165px" />
            <el-table-column prop="isFrontDisplay" label="前台能否显示" />
            <el-table-column prop="twoLevelDirectory" label="二级目录" />
            <el-table-column prop="tags" label="标签" />
            <el-table-column prop="totals" label="题目数量" />
            <el-table-column label="操作" width="240" class="btncell">
              <template #default="{ row }">
                <el-button type="text" @click="subjectCatalog(row)">
                  学科分类</el-button
                >
                <el-button type="text" @click="subjectTags(row)">
                  学科标签</el-button
                >
                <el-button type="text" @click="edit(row)">修改 </el-button>
                <el-button
                  type="text"
                  style="margin-left: 9px"
                  @click="del(row)"
                >
                  删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <elpagination
            :counts="counts"
            :data="searchData"
            :n="[5, 10, 20, 50]"
            @handleSizeChange="handleSizeChange"
            @handleCurrentChange="handleCurrentChange"
          ></elpagination>
        </div>
      </el-card>
    </div>
    <!-- 添加、修改学科的弹窗 -->
    <el-dialog
      :visible.sync="showDialog"
      width="440px"
      :title="title"
      @close="closeDialog"
    >
      <el-form ref="addRef" :model="dialogData" :rules="rules">
        <el-form-item label="学科名称" label-width="80px" prop="subjectName">
          <el-input v-model="dialogData.subjectName"></el-input>
        </el-form-item>

        <el-form-item prop="isFrontDisplay">
          <el-switch
            style="display: block"
            v-model="dialogData.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            inactive-text="是否显示"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div align="right">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="submitCheck">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, update, add, simple } from '@/api/hmmm/subjects'
import elform from '@/components/form'
import elalert from '@/components/elalert'
import elpagination from '@/components/elpagination/index.vue'
import moment from 'moment'
export default {
  data() {
    return {
      counts: 0,
      searchData: {
        subjectName: '',
        page: 1,
        pagesize: 10
      },
      list: [],
      showDialog: false,
      title: '新增学科',
      dialogData: {
        isFrontDisplay: '1',
        subjectName: null,
        id: null
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  components: { elform, elalert, elpagination },
  created() {
    this.getSubjectsList()
  },
  methods: {
    async getSubjectsList() {
      const { data } = await list(this.searchData)
      data.items.forEach(item => {
        item.isFrontDisplay = item.isFrontDisplay === 1 ? '是' : '否'
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
      })
      this.counts = data.counts
      this.list = data.items
      this.searchData.page = data.page
      this.searchData.pagesize = data.pagesize
    },
    handleSizeChange(val) {
      this.searchData.pagesize = val
      this.getSubjectsList()
    },
    handleCurrentChange(val) {
      this.searchData.page = val
      this.getSubjectsList()
    },
    search(e) {
      this.searchData.subjectName = e
      this.getSubjectsList()
    },
    add() {
      this.title = '新增学科'
      this.showDialog = true
    },
    edit(row) {
      this.title = '修改学科'
      row.isFrontDisplay === '是'
        ? (this.dialogData.isFrontDisplay = '1')
        : (this.dialogData.isFrontDisplay = '0')
      this.dialogData.subjectName = row.subjectName
      this.dialogData.id = row.id
      this.showDialog = true
    },
    async submitCheck() {
      try {
        await this.$refs.addRef.validate()
        if (this.title === '新增学科') {
          await add(this.dialogData)
          this.showDialog = false
          this.$message.success('添加成功')
          this.getSubjectsList()
        } else if (this.title === '修改学科') {
          await update(this.dialogData)
          this.showDialog = false
          this.$message.success('修改成功')
          this.getSubjectsList()
        }
      } catch (error) {
        console.log(error)
      }
    },
    closeDialog() {
      this.$refs.addRef.resetFields()
    },
    async del(row) {
      this.$confirm('该项操作不可恢复！确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.$message.success('删除成功')
          this.getSubjectsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消删除'
          })
        })
    },
    async subjectCatalog(row) {
      const { data } = await simple(row)
      data.forEach(item => {
        if (row.subjectName === item.label) row.subjectID = item.value
      })
      this.$router.push({
        path: 'directorys',
        query: { id: row.subjectID, name: row.subjectName }
      })
    },
    async subjectTags(row) {
      const { data } = await simple(row)
      data.forEach(item => {
        if (row.subjectName === item.label) row.subjectID = item.value
      })
      this.$router.push({
        path: 'tags',
        query: { id: row.subjectID, name: row.subjectName }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-card {
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  -webkit-transition: 0.3s;
  transition: 0.3s;
  border-radius: 4px;
  overflow: hidden;
  .body {
    width: 100%;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB,
      Microsoft YaHei, Arial, sans-serif;

    ::v-deep .el-table {
      .el-table--fit {
        border-right: 0;
        border-bottom: 0;
      }
    }
    ::v-deep .el-table th.el-table__cell.is-leaf {
      border-bottom: 2px solid #e8e8e8;
      background-color: #fafafa;
    }
    ::v-deep .el-card .body[data-v-eacc1416] .el-table td.el-table__cell div {
      display: flex;
      padding-left: 5px;
      padding-right: 5px;
      box-sizing: border-box;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: normal;
      word-break: break-all;
      line-height: 23px;
    }
  }
}
::v-deep .el-switch__label * {
  font-size: 14px;
  font-weight: bold;
  color: #606266;
  margin-left: 12px;
}
</style>
